package com.dimples.dd.web.framework.jackson.config;

import com.dimples.dd.common.constant.DDConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * LocalDateTime 作为 作为 RequestParam 或者 PathVariable 时，将前端传递的时间戳转换
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/8/26
 */
@Slf4j
@Configuration
public class LocalDateTimeGetConverter {

    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        log.info(DDConstant.CONFIG_LOG, "Converter [LocalDateTimeGetConverter] Config Success");

        return new Converter<String, LocalDateTime>() {
            @SuppressWarnings("NullableProblems")
            @Override
            public LocalDateTime convert(String source) {
                return LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(source)), ZoneId.systemDefault());
            }
        };
    }
}
